package com.atguigu.gmall.product.config.sharding;

import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author lfy
 * @Description
 * @create 2023-07-17 11:41
 */
@Configuration
public class ShardingJdbcDataSourceConfiguration {

    @Bean
    DataSource dataSource() throws IOException, SQLException {

        //1、拿到sharding-jdbc的配置文件
        File yamlFile = ResourceUtils.getFile("classpath:sharding-jdbc.yaml");


        //2、根据配置文件中的配置项得到数据源
        //TODO 创建数据源出现问题
        DataSource dataSource = YamlShardingSphereDataSourceFactory
                .createDataSource(yamlFile);

        return dataSource;
    }
}
